#include"chmod_tea.h"

static lv_obj_t* cont;
static lv_obj_t* title;
static lv_obj_t* tea_name_input;
static lv_obj_t* tea_name_keyboard;
static lv_obj_t* tea_price_input;
static lv_obj_t* tea_num_input;
static lv_obj_t* tea_price_keyboard;
static lv_obj_t* tea_num_keyboard;
static lv_obj_t* msgbox_s;
static int count = 0;

static void lv_messagebox_s_create(void)
{
    static const char  *tips_txt_s={"  添加成功！"};

    msgbox_s = lv_msgbox_create(lv_scr_act(),NULL,tips_txt_s,NULL,true );
    lv_obj_set_style_text_font(msgbox_s,&lv_font_source_han_sans_bold_14,0);
    lv_obj_set_size(msgbox_s,300,150);
    lv_obj_set_style_bg_opa(msgbox_s,200,0);
    lv_obj_align(msgbox_s,LV_ALIGN_CENTER,0,50);

    lv_obj_set_style_pad_top(msgbox_s,15,LV_STATE_DEFAULT);  //顶部填充
    lv_obj_t *close_btnt = lv_msgbox_get_close_btn(msgbox_s); //获取到关闭图标
    lv_obj_set_style_text_color(close_btnt,lv_color_hex(0x696969),LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(close_btnt,&lv_font_montserrat_20,0);
    lv_obj_set_style_pad_top(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_border_opa(close_btnt,0,LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_opa(close_btnt,0,LV_STATE_DEFAULT);


    lv_obj_t* msgbox_content = lv_msgbox_get_content(msgbox_s); //获取到主体
//    lv_obj_set_style_text_color(msgbox_content,lv_color_hex(0xFF0000),LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(msgbox_content,lv_color_hex(0x808080),LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(msgbox_content,10,LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(msgbox_content,5,0);
    lv_obj_set_style_text_line_space(msgbox_content,10,0);

    lv_obj_add_flag(msgbox_s,LV_OBJ_FLAG_HIDDEN);                 //默认隐藏

}
//--------------------------------回调函数区域-----------------------------------------------------*/
/*用户文本框点击事件回调*/
static void textarea_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED)  // 当文本区域获得焦点时显示键盘
    {
        lv_obj_clear_flag(tea_name_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
    else if(code == LV_EVENT_DEFOCUSED)// 当文本区域失去焦点时隐藏键盘
    {
        lv_obj_add_flag(tea_name_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
}

/*价格文本框点击事件回调*/
static void price_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED)  // 当文本区域获得焦点时显示键盘
    {
        lv_obj_clear_flag(tea_price_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
    else if(code == LV_EVENT_DEFOCUSED)// 当文本区域失去焦点时隐藏键盘
    {
        lv_obj_add_flag(tea_price_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
}

/*编号文本框点击事件回调*/
static void num_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED)  // 当文本区域获得焦点时显示键盘
    {
        lv_obj_clear_flag(tea_num_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
    else if(code == LV_EVENT_DEFOCUSED)// 当文本区域失去焦点时隐藏键盘
    {
        lv_obj_add_flag(tea_num_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
}


/*键盘按钮√的事件回调*/
static void keyboard_event_cb(lv_event_t * e)
{
     lv_event_code_t code = lv_event_get_code(e);
     if(code == LV_EVENT_READY) //当按下键盘的√按键 触发隐藏键盘
     {
        lv_obj_add_flag(tea_name_keyboard, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(tea_price_keyboard, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(tea_num_keyboard, LV_OBJ_FLAG_HIDDEN);
     }
}

// 账号注册确认事件回调函数  
#if 1
static void sure_btn_event_cb(lv_event_t*e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED)
    {
        if (count == 0)
        {
            tea_main();
            count = 1;
        }
        const char *tea_name = lv_textarea_get_text(tea_name_input);       //获取输入的奶茶名字
        const char *tea_price = lv_textarea_get_text(tea_price_input);    //获取输入的奶茶价格
        const char *tea_number = lv_textarea_get_text(tea_num_input);    //获取输入的奶茶编号

        printf("%s\t%s\t%s\n",tea_number,tea_name,tea_price);
        chmod_tea_node(head,atoi(tea_number),tea_name,atoi(tea_price));
        // lv_messagebox_s_create();                                             //建立消息框
        file_save_data(head,"tea_info.txt");
        list_for_each(head);
        lv_obj_del(cont);

    }
}
#endif

// 注册返回按钮事件回调函数  
static void return_btn_event_cb(lv_event_t*e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED)
    {
             printf("return administrator success!\n");
             lv_obj_del(cont); 
             admini_jm();                                              //系统运行界面
    }
}


//--------------------------------主函数区域-----------------------------------------------------*/
static void chmod_tea_jm_create(void)
{
    
    cont = lv_obj_create(lv_scr_act());
    lv_obj_set_size(cont, 640, 380);
    lv_obj_center(cont);

    title = lv_label_create(cont);
    lv_obj_set_size(title,220,80);
    lv_obj_align(title,LV_ALIGN_TOP_MID,40,10);

    lv_label_set_text(title, "修改奶茶信息");
    lv_obj_set_style_text_font(title,&lv_font_source_han_sans_bold_14,0);

    /*-------------------------创建一个父类 占位对象1，优化布局用---------------------*/
    lv_obj_t * tea_name_obj = lv_obj_create(cont);
    lv_obj_set_size(tea_name_obj, 250, 44);
    lv_obj_align_to(tea_name_obj, title,LV_ALIGN_BOTTOM_MID, 0, 10);
    lv_obj_set_scrollbar_mode(tea_name_obj, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能

    /* -----------------------创建奶茶名字名输入框----------------------------------------- */
    tea_name_input = lv_textarea_create(cont);
    lv_obj_set_style_text_font(tea_name_input, &lv_font_source_han_sans_bold_14, 0);
    lv_textarea_set_placeholder_text(tea_name_input, "reset teaname"); //提示文本
    lv_obj_set_size(tea_name_input, 210, 44);
    lv_textarea_set_one_line(tea_name_input, true);                    //限制为1行高度
    lv_textarea_set_max_length(tea_name_input,6);                      //限制长度为5个字符
    lv_obj_set_scrollbar_mode(tea_name_input, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能
    lv_obj_set_style_border_opa(tea_name_input,0,0);                   //边框透明为0
    lv_obj_align_to(tea_name_input,tea_name_obj, LV_ALIGN_CENTER, 5, 0);
    lv_obj_add_event_cb(tea_name_input, textarea_event_cb, LV_EVENT_ALL, NULL); //用户名输入框回调

    /*------------------------创建奶茶名键盘---------------------------------------------------*/
    tea_name_keyboard = lv_chinese_ime_create(lv_scr_act());
    lv_chinese_ime_set_text_font(&lv_font_source_han_sans_bold_14, 0);
    lv_obj_set_size(tea_name_keyboard,600,200);
    lv_obj_set_style_radius(tea_name_keyboard,20,0);
    lv_keyboard_set_textarea(tea_name_keyboard,tea_name_input);
    lv_obj_add_event_cb(tea_name_keyboard, keyboard_event_cb, LV_EVENT_READY, NULL);
    lv_obj_add_flag(tea_name_keyboard,LV_OBJ_FLAG_HIDDEN);//默认隐藏

    /*------------------------------创建一个父类 占位对象2，优化布局用------------------------*/
    lv_obj_t * tea_price_obj = lv_obj_create(cont);
    lv_obj_set_size(tea_price_obj, 250, 44);
    lv_obj_align_to(tea_price_obj,tea_name_obj, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_scrollbar_mode(tea_price_obj, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能

    /* 创建数字输入框 */
    tea_price_input = lv_textarea_create(cont);
    lv_textarea_set_placeholder_text(tea_price_input,"reset tea price");//提示文本
    lv_obj_set_scrollbar_mode(tea_price_input, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能

    lv_textarea_set_accepted_chars(tea_price_input, "0123456789"); //字符白名单
    lv_obj_set_size(tea_price_input, 210, 44);
    lv_textarea_set_one_line(tea_price_input, true);                    //限制为1行高度
    lv_obj_set_style_border_opa(tea_price_input,0,0);
    lv_obj_align_to(tea_price_input,tea_price_obj, LV_ALIGN_CENTER,5, 0);
    lv_obj_add_event_cb(tea_price_input, price_event_cb, LV_EVENT_ALL, NULL); //用户名输入框回调
    

    /*创建数量输入的键盘*/
    tea_price_keyboard =lv_keyboard_create(lv_scr_act());
    lv_obj_set_size(tea_price_keyboard,600,200);
    lv_obj_set_style_radius(tea_price_keyboard,20,0);
    lv_keyboard_set_textarea(tea_price_keyboard,tea_price_input);
    lv_keyboard_set_mode(tea_price_keyboard,LV_KEYBOARD_MODE_NUMBER); //数字键盘模式
    lv_obj_add_event_cb(tea_price_keyboard, keyboard_event_cb, LV_EVENT_READY, NULL);
    lv_obj_add_flag(tea_price_keyboard,LV_OBJ_FLAG_HIDDEN);//默认隐藏

    /*------------------------------创建一个父类 占位对象3，优化布局用------------------------*/
    lv_obj_t * tea_num_obj = lv_obj_create(cont);
    lv_obj_set_size(tea_num_obj, 250, 44);
    lv_obj_align_to(tea_num_obj,tea_price_obj, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_scrollbar_mode(tea_num_obj, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能

    /* 创建数字输入框 */
    tea_num_input = lv_textarea_create(cont);
    lv_textarea_set_placeholder_text(tea_num_input,"tea number");//提示文本
    lv_obj_set_scrollbar_mode(tea_num_input, LV_SCROLLBAR_MODE_OFF);  // 禁用滚动功能

    lv_textarea_set_accepted_chars(tea_num_input, "0123456789"); //字符白名单
    lv_obj_set_size(tea_num_input, 210, 44);
    lv_textarea_set_one_line(tea_num_input, true);                    //限制为1行高度
    lv_obj_set_style_border_opa(tea_num_input,0,0);
    lv_obj_align_to(tea_num_input,tea_num_obj, LV_ALIGN_CENTER,5, 0);
    lv_obj_add_event_cb(tea_num_input, num_event_cb, LV_EVENT_ALL, NULL); //用户名输入框回调
    

    /*创建数量输入的键盘*/
    tea_num_keyboard =lv_keyboard_create(lv_scr_act());
    lv_obj_set_size(tea_num_keyboard,600,200);
    lv_obj_set_style_radius(tea_num_keyboard,20,0);
    lv_keyboard_set_textarea(tea_num_keyboard,tea_num_input);
    lv_keyboard_set_mode(tea_num_keyboard,LV_KEYBOARD_MODE_NUMBER); //数字键盘模式
    lv_obj_add_event_cb(tea_num_keyboard, keyboard_event_cb, LV_EVENT_READY, NULL);
    lv_obj_add_flag(tea_num_keyboard,LV_OBJ_FLAG_HIDDEN);//默认隐藏

    //-----------------------------------------------------------------------------
     /* 创建返回按钮 */
     lv_obj_t * return_btn = lv_btn_create(cont);
     lv_obj_set_size(return_btn, 100, 44);
     lv_obj_align_to(return_btn,tea_num_obj, LV_ALIGN_OUT_BOTTOM_RIGHT, 3, 60);
      /*创建返回标签 */
     lv_obj_t * return_label = lv_label_create(return_btn);
     lv_label_set_text(return_label, "返回");
     lv_obj_set_style_text_font(return_label,&lv_font_source_han_sans_bold_14,0);
     lv_obj_set_style_text_color(return_label, lv_color_hex(0xffffff), 0); // 设置字体颜色为白色
     /* 设置返回事件 */
     lv_obj_add_event_cb(return_btn, return_btn_event_cb, LV_EVENT_CLICKED, NULL);

     /* 创建确认按钮 */
    lv_obj_t * sure_btn = lv_btn_create(cont);
    lv_obj_set_size(sure_btn, 100, 44);
    lv_obj_align_to(sure_btn,tea_num_obj, LV_ALIGN_OUT_BOTTOM_LEFT, -3, 60);
     /*创建按钮标签 */
    lv_obj_t * sure_label = lv_label_create(sure_btn);
    lv_label_set_text(sure_label, "确认");
    lv_obj_set_style_text_font(sure_label,&lv_font_source_han_sans_bold_14,0);
    /* 设置按钮事件 */
    lv_obj_add_event_cb(sure_btn, sure_btn_event_cb, LV_EVENT_CLICKED, NULL);
}


    
void chmod_tea_jm(void)
{
    chmod_tea_jm_create();
}